/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.editor;

import java.net.URL;
import java.util.MissingResourceException;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.uml2.uml.edit.UMLEditPlugin;

import com.cea.papyrus.core.utils.Debug;

/**
 * Common access point for image resources. This particular implementation
 * exploits the default images included in UML2 whenever possible.
 *  
 * @author frankd@ifi.uio.no
 */
public class ImageLibraryForUML2 extends ImageLibraryForEclipse {

	/** Maps from logical image name to physical image filename. */
	private static final String[] imageMap = new String[] {
		COMMENT_TOOL, "full/obj16/Comment",
		INTERACTION_TOOL, "full/obj16/Interaction",
		LIFELINE_TOOL, "full/obj16/Lifeline",
		INTERACTION_USE_TOOL, "full/obj16/InteractionUse",
		EXECUTION_SPECIFICATION_TOOL, "full/obj16/ExecutionOccurrenceSpecification",
		BEHAVIOR_EXECUTION_SPECIFICATION_TOOL, "full/obj16/BehaviorExecutionSpecification",
		ACTION_EXECUTION_SPECIFICATION_TOOL, "full/obj16/ActionExecutionSpecification",
		DESTRUCTION_EVENT_TOOL, "full/obj16/DestructionEvent",
		STATE_INVARIANT_TOOL, "full/obj16/StateInvariant",
		ACTION_TOOL, "full/obj16/Action",
		COMBINED_FRAGMENT_TOOL, "full/obj16/CombinedFragment",
		SEPARATOR_TOOL, "full/obj16/InteractionOperand",
		INTERACTION_OPERAND_GUARD_TOOL, "full/obj16/InteractionConstraint",
		CONTINUATION_TOOL, "full/obj16/Continuation",
		SIGNAL_TOOL, "full/obj16/Message_asynchSignal",
		SYNCH_CALL_TOOL, "full/obj16/Message_synchCall",
		ASYNCH_CALL_TOOL, "full/obj16/Message_asynchCall",
		REPLY_TOOL, "full/obj16/Message_reply",
		CREATE_TOOL, "full/obj16/Message_createMessage",
		DESTROY_TOOL, "full/obj16/Message_deleteMessage",
	};

	protected String lookupImagePath(String imageId) {
		for (int i = 0; i < imageMap.length; i += 2)
			if (imageId.equals(imageMap[i]))
				return imageMap[i + 1];
		return null;
	}

	@Override
	public ImageDescriptor getImage(String imageId) {
		if (imageId == null)
			throw new IllegalArgumentException();
		
		ImageDescriptor image = null;
		final String relativePath = lookupImagePath(imageId);
		if (relativePath != null) {
			try {
				Object ref = UMLEditPlugin.INSTANCE.getImage(relativePath);
				if (ref instanceof URL)
					image = ImageDescriptor.createFromURL((URL) ref);
			}
			catch (MissingResourceException e) {
				Debug.log(e);
			}
			if (image == null)
				 image = super.getImage(imageId);	
		}
		return image; 
	}
	
}
